package com.tqz.juc.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2022/2/8 20:15
 */
public class ThreadPoolExecutorTest {
    
    private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);
    
    
    public static void main(String[] args) {
        System.out.println(getSuitableThreadCount(1));
    
        boolean flag = false || true;
    
        System.out.println(flag);
    
//        getTask();
        
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 60,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
        
        for (int i = 0; i < 10; i ++) {
            executor.submit(() -> {
                System.out.println("1");
            });
        }
    
        executor.shutdown(); // running -> shutdown
        executor.shutdownNow(); // running -> stop
    }
    
    private static Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?
        
        for (;;) {
            
            
            // Are workers subject to culling?
            boolean timed = false || 10 > 5;
            
            try {
                Runnable r = timed ?
                        workQueue.poll(10, TimeUnit.NANOSECONDS) :
                        workQueue.take();
                if (r != null) {
                    return r;
                }
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            } finally {
                // ignore
            }
    
            System.out.println("111");
        }
    }
    
    public static int getSuitableThreadCount(int threadMultiple) {
        final int coreCount = Runtime.getRuntime().availableProcessors();
        int workerCount = 1;
        while (workerCount < coreCount * threadMultiple) {
            workerCount <<= 1;
        }
        return workerCount;
    }
}
